import hurryoff.log as log
from hurryoff.db import metadata
from hurryoff.spiders.trains.uz import UzTrainsSpider, UzParsingException

@log.functionTracer
def recreateDB(request):
    metadata.drop_all(metadata.bind)
    metadata.create_all(metadata.bind)

@log.functionTracer
def addTrain(request):
    if ('train_number' in request.params) and request.params['train_number']:
        trainNumber = request.params['train_number']
        spider = UzTrainsSpider(echo=True)
        try:
            spider.processTrain(trainNumber)
        except UzParsingException as e:
            if e.errorMsg:
                return e.errorMsg
            return e.warningMsg
    else:
        return "Required parameter 'train_number' not found or empty"
